package com.rtwo.game.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.rtwo.game.myplane.Art;
import com.rtwo.game.myplane.MyPlane;


public class LoadingScreen implements Screen{
	
	private int			width;
	private int			height;
	private float		statetime;
	private boolean		init_over;
	
	private MyPlane game;
	private Stage	stage;
	private Button	btn_goback;
	private Actor	actor_loading;
	private Actor	actor_title;
    private OrthographicCamera cam;
	private Viewport viewport;
	
	public LoadingScreen(MyPlane game) {
		this.game = game;
	}

    //当Screen获得焦点的时候调用
    @Override
    public void show() {

        init_over	= false;

        Art.init();
        Art.load();

        statetime = 0;

        width = Gdx.graphics.getWidth();
        height = Gdx.graphics.getHeight();

        SpriteBatch batch = new SpriteBatch();
        cam = new OrthographicCamera();
		viewport = new FitViewport(width,height,cam);
        cam.translate(width/2,height/2);
        cam.update();
        stage = new Stage(viewport,batch);
		Gdx.input.setInputProcessor(stage);
        actor_loading = new Actor(){
			@Override
			public void draw(Batch batch, float parentAlpha) {
				if (!Art.isLoaded){
					batch.draw(Art.animation_gameLoading.getKeyFrame(statetime, true),
							width / 3, height / 2);
				}
			}
        };
        stage.addActor(actor_loading);
    }

	@Override
	public void render(float delta) {
		Art.update();
		init();
        cam.update();
	    statetime += Gdx.graphics.getDeltaTime();
	    if (statetime > 5){
	    	game.setScreen(new GameScreen());
	    	return;
	    }

	    if (!init_over){
	    	Gdx.gl.glClearColor(0, 0, 0, 0);
		    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
	    }
	    else{
	    	Gdx.gl.glClearColor(1, 1, 1, 1);
	    	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
	    }
		stage.act();
	    stage.draw();

//		SpriteBatch batch = new SpriteBatch();
//		batch.setProjectionMatrix(cam.combined);
//		batch.begin();
//		batch.draw(new Texture(Gdx.files.internal("droplet.png")),100,100);
//		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		viewport.update(width,height);
	}

	private void init(){
		if (init_over)return;

		if (Art.isLoaded){
			actor_title	= new Actor(){
				public void draw (SpriteBatch batch, float parentAlpha) {
					if (Art.isLoaded){
						batch.draw(Art.backgroud, 0, 0);
						batch.draw(Art.copyright, width / 2 - Art.copyright.getRegionWidth() / 2,
			    			height / 2 );
					}
				}
			};
			stage.addActor(actor_title);
			actor_loading.remove();
			
			btn_goback	= new Button(new TextureRegionDrawable(Art.btn_goback) );
			btn_goback.setPosition(width / 2 ,Art.btn_goback.getRegionHeight());
			btn_goback.addListener(new InputListener(){
				public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
					Gdx.app.exit();
					super.touchUp(event, x, y, pointer, button);
				}
				public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
					return true;
				}
			});
			
			stage.addActor(btn_goback);
			Gdx.input.setInputProcessor(stage);
			init_over = true;
		}
	}
	
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
	}
}
